// 放大缩小
function zoomInAndOut() {
  $('.zoom-in').on('click', function() {
    if (map.getZoom() < map.getMaxZoom()) {
      map.setZoom(map.getZoom() + 1);
    }
  });
  $('.zoom-out').on('click', function() {
    if (map.getZoom() > map.getMinZoom()) {
      map.setZoom(map.getZoom() - 1);
    }
  });
}

//绑定工具栏事件
function bindEvent() {
  //矢量
  $('#roadnetworktool').on('click', function() {
    $(this).css('color', '#2C9EFF');
    $('#planettool').css('color', '#000');
    roadlayer.show();
    annolayer.show();
    imglayer.hide();
    imgannolayer.hide();
  });
  //影像
  $('#planettool').on('click', function() {
    $(this).css('color', '#2C9EFF');
    $('#roadnetworktool').css('color', '#000');
    roadlayer.hide();
    annolayer.hide();
    imglayer.show();
    imgannolayer.show();
  });
  //全图
  $('#fullextenttool').on('click', function() {
    // 显示范围设置
    map.setExtent(startExtent);
  });
  //测距
  $('#distancetool').on('click', function() {
    drawType = 1;
    activeTool('POLYLINE');
  });
  //面积
  $('#areatool').on('click', function() {
    drawType = 1;
    activeTool('POLYGON');
  });
  //框选
  $('#rectangletool').on('click', function() {
    drawType = 2;
    activeTool('RECTANGLE');
  });
  //自由选择
  $('#polygontool').on('click', function() {
    drawType = 2;
    activeTool('POLYGON');
  });
  //清除
  $('#cleartool').on('click', function() {
    map.graphics.clear();
  });
}

//
function createToolbar() {
  require(['esri/toolbars/draw'], function(Draw) {
    toolbar = new Draw(map);
    toolbar.on('draw-end', drawToMap);
  });
}

function activeTool(gettype) {
  require(['esri/toolbars/draw'], function(Draw) {
    toolbar.activate(Draw[gettype]);
  });
}

function drawToMap(event) {
  switch (drawType) {
    case 1:
      measureMap(event);
      break;
    case 2:
      addGraphicToMap(event);
      break;
  }
}

function addGraphicToMap(event) {
  require([
    'esri/symbols/SimpleMarkerSymbol',
    'esri/symbols/SimpleLineSymbol',
    'esri/symbols/SimpleFillSymbol'
  ], function(SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol) {
    var symbol;
    toolbar.deactivate();
    var text = '';
    var point;
    switch (event.geometry.type) {
      case 'point':
      case 'multipoint':
        symbol = new SimpleMarkerSymbol();
        break;
      case 'polyline':
        symbol = new esri.symbol.SimpleLineSymbol(
          esri.symbol.SimpleLineSymbol.STYLE_SOLID,
          new esri.Color([240, 197, 54]),
          2
        );
        break;
      default:
        symbol = new esri.symbol.SimpleFillSymbol(
          esri.symbol.SimpleFillSymbol.STYLE_SOLID,
          new esri.symbol.SimpleLineSymbol(
            esri.symbol.SimpleLineSymbol.STYLE_SOLID,
            new esri.Color([255, 255, 255]),
            2
          ),
          new esri.Color([240, 197, 54, 0.5])
        );
        break;
    }
    var graphic = new esri.Graphic(event.geometry, symbol);
    map.graphics.add(graphic);
  });
}

function measureMap(event) {
  require([
    'esri/symbols/SimpleMarkerSymbol',
    'esri/symbols/SimpleLineSymbol',
    'esri/symbols/SimpleFillSymbol',
    'esri/geometry/geometryEngine',
    'esri/symbols/TextSymbol',
    'esri/Color',
    'esri/symbols/Font'
  ], function(
    SimpleMarkerSymbol,
    SimpleLineSymbol,
    SimpleFillSymbol,
    geometryEngine,
    TextSymbol,
    Color,
    Font
  ) {
    var symbol;
    toolbar.deactivate();
    var text = '';
    var point;
    switch (event.geometry.type) {
      case 'point':
      case 'multipoint':
        symbol = new SimpleMarkerSymbol();
        break;
      case 'polyline':
        symbol = new esri.symbol.SimpleLineSymbol(
          esri.symbol.SimpleLineSymbol.STYLE_SOLID,
          new esri.Color([240, 197, 54]),
          2
        );
        break;
      default:
        symbol = new esri.symbol.SimpleFillSymbol(
          esri.symbol.SimpleFillSymbol.STYLE_SOLID,
          new esri.symbol.SimpleLineSymbol(
            esri.symbol.SimpleLineSymbol.STYLE_SOLID,
            new esri.Color([255, 255, 255]),
            2
          ),
          new esri.Color([240, 197, 54, 0.5])
        );
        break;
    }
    var graphic = new esri.Graphic(event.geometry, symbol);
    map.graphics.add(graphic);

    var geom = deepClone(event.geometry);
    switch (geom.type) {
      case 'polyline':
        geom.spatialReference = {
          wkid: 4326
        };
        text = geometryEngine.geodesicLength(geom, 'meters');
        if (text > 1000) {
          text = (text / 1000).toFixed(2) + '千米';
        } else {
          text = text.toFixed(2) + '米';
        }
        var paths = geom.paths;
        point = new esri.geometry.Point(paths[0][paths[0].length - 1][0], paths[0][paths[0].length - 1][1]);
        break;
      default:
        geom.spatialReference = {
          wkid: 4326
        };
        text = geometryEngine.geodesicArea(geom, 'square-meters');
        if (text > 1000000) {
          text = (text / 1000000).toFixed(2) + '平方千米';
        } else {
          text = text.toFixed(2) + '平方米';
        }
        var rings = geom.rings;
        point = new esri.geometry.Point(rings[0][rings[0].length - 2][0], rings[0][rings[0].length - 2][1]);
        break;
    }

    var font = new Font();
    font.setSize('12pt');
    font.setWeight(Font.WEIGHT_BOLD);
    var textymbol = new esri.symbol.TextSymbol(text)
      .setColor(new esri.Color([128, 0, 0]))
      .setAlign(esri.symbol.Font.ALIGN_MIDDLE)
      .setFont(new esri.symbol.Font('18pt').setWeight(esri.symbol.Font.WEIGHT_BOLD));
    var textgraphic = new esri.Graphic(point, textymbol);
    map.graphics.add(textgraphic);
  });
}

//深拷贝
function deepClone(data) {
  var type = getType(data);
  var obj;
  if (type === 'array') {
    obj = [];
  } else if (type === 'object') {
    obj = {};
  } else {
    //不再具有下一层次
    return data;
  }
  if (type === 'array') {
    for (var i = 0, len = data.length; i < len; i++) {
      obj.push(deepClone(data[i]));
    }
  } else if (type === 'object') {
    for (var key in data) {
      obj[key] = deepClone(data[key]);
    }
  }
  return obj;
}

function getType(obj) {
  //tostring会返回对应不同的标签的构造函数
  var toString = Object.prototype.toString;
  var map = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object'
  };
  if (obj instanceof Element) {
    return 'element';
  }
  return map[toString.call(obj)];
}
